package view;

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

import model.Pixmap;

/**
 * 
 * @author Andrea Scripa
 * 
 */
public class Canvas extends JPanel {
	private Pixmap myPixmap;

	/**
	 * Create canvas within the given Frame and with the given size.
	 */
	public Canvas(Dimension size) {
		setBorder(BorderFactory.createLoweredBevelBorder());
	}

	/**
	 * Refresh the contents of the canvas by updating its size and pixmap.
	 */
	public void refresh(Pixmap pixmap) {
		myPixmap = pixmap;
		if (!myPixmap.getSize().equals(getSize())) {
			setSize(myPixmap.getSize());
		}
		repaint();
	}

	@Override
	public void paintComponent(Graphics paintbrush) {
		super.paintComponent(paintbrush);
		if (myPixmap != null)
			myPixmap.paint(paintbrush);
	}
}
